<script setup lang="ts">
import { getFormatDictMap } from '@/util/dict'
import { defineAsyncComponent, onMounted, ref } from 'vue'
import { Search, Refresh, icon } from '@/util/icons'
import type { MenuTable } from '@/types/menu'
import { getSysMenus } from '@/api/menu'
import type { MenuManageData } from '@/types/menu'
import { formatMenuManage } from '@/util/menu'
import AuthButton from '@/components/AuthButton.vue'
import AuthLink from '@/components/AuthLink.vue'
const tableData = ref<MenuManageData[]>([])
// 缓存数据 用于 页面前端搜索
const baseData = ref<MenuTable[]>([])
/* 获取对应的字典信息 */
const tableDict = ref<Map<string, string>>()
const statusDict = ref<Map<string, string>>()
const faceDict = ref<Map<string, string>>()
// 页面加载
const loading = ref<boolean>(false)
const pageN = '菜单'
// 获取页面数据
function getPageData() {
  loading.value = true
  getSysMenus(searchForm.value)
    .then((data) => {
      baseData.value = data.data
      baseData.value.sort((d1, d2) => {
        return (d1.sort || 1) - (d2.sort || 1)
      })
      tableData.value = formatMenuManage(baseData.value)
    })
    .finally(() => {
      setTimeout(() => {
        loading.value = false
      }, 500)
    })
}

onMounted(() => {
  getFormatDictMap('sys_menu_manage', 'head').then((d) => {
    tableDict.value = d
  })
  getFormatDictMap('sys_menu_status', 'val').then((d) => {
    statusDict.value = d
  })
  getFormatDictMap('sys_menu_face', 'val').then((d) => {
    faceDict.value = d
  })
  getPageData()
})

/* 搜索 */
/* 是否展示搜索栏 */
const showSearchBar = ref<boolean>(true)
const searchForm = ref<MenuTable>({
  name: '',
  key: '',
  path: '',
  status: ''
})

// 重置搜索框
function reSearch() {
  searchForm.value = {
    name: '',
    key: '',
    path: '',
    status: ''
  }
  tableData.value = formatMenuManage(baseData.value)
}
// 搜索
function search() {
  tableData.value = baseData.value.filter((d) => {
    if (!d.name!.includes(searchForm.value.name!)) {
      return false
    }
    if (searchForm.value.status != '' && d.status != searchForm.value.status) {
      return false
    }
    return true
  })
}

/* 对话框控制 */
const dialogVisible = ref<boolean>(false)

/* 选择器 */
const selectData = ref<MenuTable[]>([])
function onSelect(data: MenuTable[]) {
  selectData.value = data
}
const delData = ref<MenuTable[]>([])
/* 控制器 */
const controlType = ref<'none' | 'del' | 'add' | 'edit' | 'editMenuHandle'>('none')
const dialogTitle = ref('')
// 删除控制
const Del = defineAsyncComponent(() => import('./Del.vue'))
function del(rows: MenuTable[]) {
  controlType.value = 'del'
  dialogTitle.value = `你确定要删除以下${pageN}吗?`
  delData.value = rows
  dialogVisible.value = true
}
// 添加控制
const Add = defineAsyncComponent(() => import('./Add.vue'))
function add() {
  controlType.value = 'add'
  dialogTitle.value = `添加${pageN}`
  dialogVisible.value = true
}

//编辑控制
const Edit = defineAsyncComponent(() => import('./Edit.vue'))
const EditData = ref<MenuTable>()
function edit(d: MenuTable) {
  EditData.value = d
  controlType.value = 'edit'
  dialogTitle.value = `修改${pageN}`
  dialogVisible.value = true
}

//菜单按钮编辑控制
const EditMenuHandle = defineAsyncComponent(() => import('./EditMenuHandle.vue'))
function editMenuHandle(d: MenuTable) {
  EditData.value = d
  controlType.value = 'editMenuHandle'
  dialogTitle.value = `修改${pageN}按钮`
  dialogVisible.value = true
}
</script>

<template>
  <div class="page-root">
    <div class="control">
      <div class="search" v-show="showSearchBar">
        <el-form :inline="true">
          <el-form-item :label="tableDict?.get('name') || 'name'">
            <el-input
              v-model="searchForm.name"
              :placeholder="'请输入' + tableDict?.get('name') || 'name'"
            ></el-input>
          </el-form-item>
          <el-form-item :label="tableDict?.get('status') || 'status'">
            <el-select
              v-model="searchForm.status"
              :placeholder="'请选择' + tableDict?.get('status') || 'status'"
            >
              <el-option label="无" value="" />
              <el-option :label="statusDict?.get('1') || '1'" value="1" />
              <el-option :label="statusDict?.get('0') || '0'" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="search">搜索</el-button>
            <el-button :icon="Refresh" @click="reSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="ctl-bts">
        <span class="ctl-l">
          <AuthButton @click="add" auth-tag="sys:menu:add"></AuthButton>
          <AuthButton
            @click="edit(selectData[0])"
            :disabled="selectData.length != 1"
            auth-tag="sys:menu:edit"
          >
          </AuthButton>
          <AuthButton
            @click="del(selectData)"
            :disabled="selectData.length == 0"
            auth-tag="sys:menu:del"
          />
        </span>
        <span class="ctl-r">
          <el-button :icon="Search" circle @click="showSearchBar = !showSearchBar"></el-button>
          <el-button :icon="Refresh" @click="getPageData" circle></el-button>
        </span>
      </div>
    </div>
    <div class="ctx">
      <el-table
        v-loading="loading"
        table-layout="auto"
        :stripe="true"
        :data="tableData"
        height="100%"
        row-key="mId"
        @selection-change="onSelect"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column width="150px" prop="name" align="center">
          <template #header>
            {{ tableDict?.get('name') || 'name' }}
          </template>
          <template #default="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="icon" align="center">
          <template #header>
            {{ tableDict?.get('icon') || 'icon' }}
          </template>
          <template #default="{ row }">
            <el-icon v-if="row.iconType != 'none'">
              <component :is="(icon as any)[row.icon]" />
            </el-icon>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="path" align="center">
          <template #header>
            {{ tableDict?.get('path') || 'path' }}
          </template>
          <template #default="{ row }">
            <AuthLink auth-tag="sys:menu:edit_button" @click="editMenuHandle(row)">{{
              row['path']
            }}</AuthLink>
          </template>
        </el-table-column>
        <el-table-column prop="iconType" align="center">
          <template #header>
            {{ tableDict?.get('iconType') || 'iconType' }}
          </template>
          <template #default="{ row }">
            <span>{{ row.iconType }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sort" align="center">
          <template #header>
            {{ tableDict?.get('sort') || 'sort' }}
          </template>
          <template #default="{ row }">
            <span class="highlight-info">{{ row.sort }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center">
          <template #header>
            {{ tableDict?.get('status') || 'status' }}
          </template>
          <template #default="{ row }">
            <el-tag :type="row['status'] === '1' ? 'success' : 'danger'">{{
              statusDict?.get(row['status']) || row['status']
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="component" align="center">
          <template #header>
            {{ tableDict?.get('component') || 'component' }}
          </template>
        </el-table-column>
        <el-table-column align="center">
          <template #header> 操作 </template>
          <template #default="{ row }">
            <div class="buttons">
              <AuthLink auth-tag="sys:menu:edit" @click="edit(row)" />
              <AuthLink auth-tag="sys:menu:del" @click="del([row])" />
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      @closed="controlType = 'none'"
      v-model="dialogVisible"
      :title="dialogTitle"
      :width="controlType == 'editMenuHandle' ? '90%' : '600px'"
    >
      <Del
        v-if="controlType == 'del'"
        :refresh="getPageData"
        :table-dict="tableDict!"
        :rows="delData"
        @c-dlg="dialogVisible = false"
      />
      <Add
        v-else-if="controlType == 'add'"
        :refresh="getPageData"
        :menus="baseData"
        :d-map="tableDict!"
        @c-dlg="dialogVisible = false"
      />
      <Edit
        v-else-if="controlType == 'edit'"
        :refresh="getPageData"
        :d-map="tableDict!"
        :menus="baseData"
        :edit-data="EditData!"
        @c-dlg="dialogVisible = false"
      />
      <EditMenuHandle
        v-else-if="controlType == 'editMenuHandle'"
        :edit-data="EditData!"
        @c-dlg="dialogVisible = false"
      />
    </el-dialog>
  </div>
</template>
<style scoped lang="less">
@import '../../../assets/page_bgl.less';
</style>
